/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zcp.notepad;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

import java.util.Calendar;

/**
 * A dialog that prompts the user for the time of day using a {@link TimePicker}.
 *
 * <p>See the <a href="{@docRoot}resources/tutorials/views/hello-timepicker.html">Time Picker
 * tutorial</a>.</p>
 */
public class MyTimePickerDialog extends AlertDialog implements OnClickListener, 
        OnTimeChangedListener {

    /**
     * The callback interface used to indicate the user is done filling in
     * the time (they clicked on the 'Set' button).
     */
    public interface OnTimeSetListener {
       
        /**
         * @param view The view associated with this listener.
         * @param hourOfDay The hour that was set.
         * @param minute The minute that was set.
         */
        void onTimeSet(TimePicker view, int year, int monthOfYear, int dayOfMonth,int hourOfDay, int minute);
    }

    private static final String HOUR = "hour";
    private static final String MINUTE = "minute";
    private static final String IS_24_HOUR = "is24hour";
    
    private final TimePicker mTimePicker;
    private final OnTimeSetListener mCallback;
    private final Calendar mCalendar;
    private final java.text.DateFormat mDateFormat;
    private final java.text.DateFormat mTitleDateFormat;
    
    private Button date_but;
    
    
    int mInitialYear;
    int mInitialMonthOfYear;//10月的值是9
    int mInitialDayOfMonth;
    int mInitialHourOfDay;
    int mInitialMinute;
    boolean mIs24HourView;

   /**
     * @param context Parent.
     * @param theme the theme to apply to this dialog
     * @param callBack How parent is notified.
     * @param hourOfDay The initial hour.
     * @param minute The initial minute.
     * @param is24HourView Whether this is a 24 hour view, or AM/PM.
     */
    public MyTimePickerDialog(final Context context,
            OnTimeSetListener callBack,
            int hourOfDay, int minute, boolean is24HourView,int year, int monthOfYear, int dayOfMonth) {
        super(context);
        mCallback = callBack;
        mInitialHourOfDay = hourOfDay;
        mInitialMinute = minute;
        mIs24HourView = is24HourView;
        mInitialYear=year;
        mInitialMonthOfYear=monthOfYear;//10月的值是9
        mInitialDayOfMonth=dayOfMonth;

        mTitleDateFormat = java.text.DateFormat.
        getDateInstance(java.text.DateFormat.FULL);
        mDateFormat = DateFormat.getTimeFormat(context);
        mCalendar = Calendar.getInstance();
//        setIcon(android.R.drawable.ic_dialog_info);
        
        setButton(context.getText(R.string.date_time_set), this);
        setButton2(context.getText(R.string.Cancel), (OnClickListener) null);
       
        LayoutInflater inflater = 
                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.my_time_picker_dialog, null);
        setView(view);
        date_but=(Button) view.findViewById(R.id.but_date);
        //设置打开
        date_but.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                openDateDialog(context,mInitialYear,mInitialMonthOfYear,mInitialDayOfMonth);                
            }
        });
        updateTitle(mInitialHourOfDay, mInitialMinute);
        
        mTimePicker = (TimePicker) view.findViewById(R.id.timePicker);

        // initialize state
        mTimePicker.setCurrentHour(mInitialHourOfDay);
        mTimePicker.setCurrentMinute(mInitialMinute);
        mTimePicker.setIs24HourView(mIs24HourView);
        mTimePicker.setOnTimeChangedListener(this);
        
        
    }
    
    public void onClick(DialogInterface dialog, int which) {
        if (mCallback != null) {
            mTimePicker.clearFocus();
            mCallback.onTimeSet(mTimePicker, mCalendar.get(Calendar.YEAR),mCalendar.get(Calendar.MONTH),mCalendar.get(Calendar.DAY_OF_MONTH),mTimePicker.getCurrentHour(), 
                    mTimePicker.getCurrentMinute());
        }
    }

    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
        updateTitle(hourOfDay, minute);
    }
    
    public void updateTime(int hourOfDay, int minutOfHour) {
        mTimePicker.setCurrentHour(hourOfDay);
        mTimePicker.setCurrentMinute(minutOfHour);
    }

    private void updateTitle(int hour, int minute) {
        mCalendar.set(Calendar.HOUR_OF_DAY, hour);
        mCalendar.set(Calendar.MINUTE, minute);
        String title=mTitleDateFormat.format(mCalendar.getTime())+mDateFormat.format(mCalendar.getTime());
        this.setTitle(title);        
    }
    
    private void updateTitle(int year, int monthOfYear, int dayOfMonth) {
        mCalendar.set(Calendar.YEAR, year);
        mCalendar.set(Calendar.MONTH, monthOfYear);
        mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        String title=mTitleDateFormat.format(mCalendar.getTime())+mDateFormat.format(mCalendar.getTime());
        this.setTitle(title);
    }
    
    @Override
    public Bundle onSaveInstanceState() {
        Bundle state = super.onSaveInstanceState();
        state.putInt(HOUR, mTimePicker.getCurrentHour());
        state.putInt(MINUTE, mTimePicker.getCurrentMinute());
        state.putBoolean(IS_24_HOUR, mTimePicker.is24HourView());
        return state;
    }
    
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        int hour = savedInstanceState.getInt(HOUR);
        int minute = savedInstanceState.getInt(MINUTE);
        mTimePicker.setCurrentHour(hour);
        mTimePicker.setCurrentMinute(minute);
        mTimePicker.setIs24HourView(savedInstanceState.getBoolean(IS_24_HOUR));
        mTimePicker.setOnTimeChangedListener(this);
        updateTitle(hour, minute);
    }
    
    /**
     * 打开日期设置对话框。
     */
    private void openDateDialog(Context context, int year, int monthOfYear, int dayOfMonth) {
        DatePickerDialog dialog = new DatePickerDialog(context, listener, year, monthOfYear,
                dayOfMonth);
//        dialog.setIcon(android.R.drawable.ic_dialog_info);
        dialog.setIcon(0);
        dialog.show();
    }

    private DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            updateTitle(year,monthOfYear,dayOfMonth);
        }

    };

}
